package com.whl215.kit.grape.ui

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.LinearLayoutManager
import com.whl215.kit.adapter.GrapeBaseAdapter
import com.whl215.kit.adapter.element.GrapeAction
import com.whl215.kit.adapter.element.GrapeElement
import com.whl215.kit.adapter.element.ListStatusGrapeElement
import com.whl215.kit.adapter.element.LoadMoreGrapeElement
import com.whl215.kit.grape.R
import com.whl215.kit.grape.adapter.UserAdapter
import com.whl215.kit.grape.databinding.ActivityQuickAdapterBinding
import com.whl215.kit.grape.model.UserBean
import com.whl215.kit.grape.tools.ToastTool

/**
 * author： happiness
 * date： 2021/5/4
 * description：GrapeQuickAdapter 例子
 */
class QuickAdapterActivity : AppCompatActivity(), View.OnClickListener {

    private lateinit var mViewBinding: ActivityQuickAdapterBinding
    private lateinit var mUserAdapter: UserAdapter
    private var pageIndex = 0
    private var totalPage = 4
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mViewBinding = ActivityQuickAdapterBinding.inflate(layoutInflater)
        setContentView(mViewBinding.root)
        mUserAdapter = UserAdapter()
        mViewBinding.rvContent.layoutManager = LinearLayoutManager(this)
        mViewBinding.rvContent.adapter = mUserAdapter
        mViewBinding.btnAddData.setOnClickListener(this)
        mViewBinding.btnLoadData.setOnClickListener(this)
        setUpAdapter()
    }


    /**
     * 添加模拟数据
     */
    private fun fetchData(count: Int): MutableList<UserBean> {
        val list = mutableListOf<UserBean>()
        for (i in 0 until count) {
            val id = "${pageIndex}${i}"
            list.add(
                UserBean(
                    id,
                    "我是${id}",
                    (0..100).random(),
                    R.mipmap.ic_launcher_round
                )
            )
        }
        pageIndex++
        return list
    }

    /**
     * 设置adapter
     */
    private fun setUpAdapter() {
        //列表项点击事件
        mUserAdapter.onItemClickListener = object : GrapeBaseAdapter.OnItemClickListener {
            override fun onItemClick(adapter: GrapeBaseAdapter<*>, view: View, position: Int) {
                val item = mUserAdapter.getItem(position)
                ToastTool.showToast(view.context, "点击${item.name}")
            }
        }
        //列表项长按事件
        mUserAdapter.onItemLongClickListener = object : GrapeBaseAdapter.OnItemLongClickListener {
            override fun onItemLongClick(
                adapter: GrapeBaseAdapter<*>, view: View, position: Int
            ): Boolean {
                val item = mUserAdapter.getItem(position)
                ToastTool.showToast(view.context, "长按${item.name}")
                return true
            }
        }
        //设置列表项中 某一个控件的点击事件
        mUserAdapter.onChildClickListener = object : GrapeBaseAdapter.OnChildClickListener {
            override fun onChildClick(adapter: GrapeBaseAdapter<*>, view: View, position: Int) {
                val item = mUserAdapter.getItem(position)
                when (view.id) {
                    R.id.item_tv_name -> {
                        ToastTool.showToast(view.context, "点击${item.name}的名字")
                    }
                    R.id.item_tv_age -> {
                        ToastTool.showToast(view.context, "点击${item.name}的年龄")
                    }
                    R.id.item_img_head_portrait -> {
                        ToastTool.showToast(view.context, "点击${item.name}的头像")
                    }
                }
            }
        }
        //设置监听 即是开启加载更多
        mUserAdapter.loadMoreElement.onLoadMoreListener =
            object : LoadMoreGrapeElement.OnLoadMoreListener {
                override fun onLoadMore() {
                    if (pageIndex >= totalPage) {
                        mUserAdapter.loadMoreAction = GrapeAction.buildLoadMoreCompletionAction()
                    } else {
                        mViewBinding.rvContent.postDelayed({
                            mUserAdapter.addData(list = fetchData(10))
                            mUserAdapter.loadMoreAction = GrapeAction.buildLoadMoreEndAction()
                        }, 2000)

                    }
                }
            }
        //设置列表状态 element的 错误点击回调
        mUserAdapter.listStatusElement.onErrorListener = object : GrapeElement.OnErrorListener {
            override fun onError() {
                mUserAdapter.listStatusAction = GrapeAction.buildListLoadAction()
                mViewBinding.rvContent.postDelayed({
//                    mViewBinding.rvContent.scrollToPosition(0)
                    mUserAdapter.setData(fetchData(10))
                }, 2000)
            }
        }
        //设置加载更多 element的 错误点击回调
//        mUserAdapter.loadMoreElement.onErrorListener = object :GrapeElement.OnErrorListener{
//            override fun onError() {
//                mUserAdapter.listStatusAction = GrapeAction.buildListLoadAction()
//                mViewBinding.rvContent.postDelayed({
//                    fetchData(10)
//                },2000)
//            }
//        }
    }


    override fun onClick(v: View?) {
        if (v == mViewBinding.btnAddData) {
            mUserAdapter.addData(list = fetchData(10))
        } else if (v == mViewBinding.btnLoadData) {
            //设置列表状态为 正在加载
            mUserAdapter.listStatusAction = GrapeAction.buildListLoadAction()
            mViewBinding.rvContent.postDelayed({
                //设置列表状态为 错误
                mUserAdapter.listStatusAction = GrapeAction.buildListErrorAction()
            }, 2000)
        }
    }

}